package cn.iocoder.yudao.module.common.service.gradesubjecttextbookversion;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.gradesubjecttextbookversion.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.gradesubjecttextbookversion.GradeSubjectTextbookVersionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.common.dal.mysql.gradesubjecttextbookversion.GradeSubjectTextbookVersionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.common.enums.ErrorCodeConstants.*;

/**
 * 年级学科版本 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GradeSubjectTextbookVersionServiceImpl implements GradeSubjectTextbookVersionService {

    @Resource
    private GradeSubjectTextbookVersionMapper gradeSubjectTextbookVersionMapper;

    @Override
    public Long createGradeSubjectTextbookVersion(GradeSubjectTextbookVersionSaveReqVO createReqVO) {
        // 插入
        GradeSubjectTextbookVersionDO gradeSubjectTextbookVersion = BeanUtils.toBean(createReqVO, GradeSubjectTextbookVersionDO.class);
        gradeSubjectTextbookVersionMapper.insert(gradeSubjectTextbookVersion);
        // 返回
        return gradeSubjectTextbookVersion.getId();
    }

    @Override
    public void updateGradeSubjectTextbookVersion(GradeSubjectTextbookVersionSaveReqVO updateReqVO) {
        // 校验存在
        validateGradeSubjectTextbookVersionExists(updateReqVO.getId());
        // 更新
        GradeSubjectTextbookVersionDO updateObj = BeanUtils.toBean(updateReqVO, GradeSubjectTextbookVersionDO.class);
        gradeSubjectTextbookVersionMapper.updateById(updateObj);
    }

    @Override
    public void deleteGradeSubjectTextbookVersion(Long id) {
        // 校验存在
        validateGradeSubjectTextbookVersionExists(id);
        // 删除
        gradeSubjectTextbookVersionMapper.deleteById(id);
    }

    private void validateGradeSubjectTextbookVersionExists(Long id) {
        if (gradeSubjectTextbookVersionMapper.selectById(id) == null) {
            throw exception(GRADE_SUBJECT_TEXTBOOK_VERSION_NOT_EXISTS);
        }
    }

    @Override
    public GradeSubjectTextbookVersionDO getGradeSubjectTextbookVersion(Long id) {
        return gradeSubjectTextbookVersionMapper.selectById(id);
    }

    @Override
    public PageResult<GradeSubjectTextbookVersionDO> getGradeSubjectTextbookVersionPage(GradeSubjectTextbookVersionPageReqVO pageReqVO) {
        return gradeSubjectTextbookVersionMapper.selectPage(pageReqVO);
    }

}